package com.huazi.www.yang.medicine.uilt;

import java.sql.*;

public class BaseDao {
    private String driver = "com.mysql.cj.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=utf8";
    private String user = "root";
    private String password = "123456";
    Connection conn = null;


//    private static String driver;
//    private static String url;
//    private static String user;
//    private static String password;
//    Connection conn=null;

//    static
//    {
//        init();
//    }

    public Connection getconnection() {

        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return conn;

    }

    public void closell(Connection conn, PreparedStatement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO: handle exception
            }
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                // TODO: handle exception
            }
        }
        if (rs != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO: handle exception
            }
        }
    }

    public int executeupdate(String sql, Object... param) {
        int num = 0;
        conn = this.getconnection();
        PreparedStatement p = null;
        try {
            p = conn.prepareStatement(sql);
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    p.setObject(i + 1, param[i]);
                }
            }
            num = p.executeUpdate();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            this.closell(conn, p, null);
        }
        return num;

    }

    public ResultSet executeselect(String sql, Object... ob) {
        Connection connection = getconnection();
        ResultSet rs = null;
        try {
            PreparedStatement pst = connection.prepareStatement(sql);
            for (int i = 0; i < ob.length; i++) {
                pst.setObject(i + 1, ob[i]);
            }
            System.out.println(sql);
            rs = pst.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return rs;
    }


//    public static  void init()
//    {
//        Properties params=new Properties();
//        String configFile="database.properties";
//        InputStream is=user_gj.class.getClassLoader().getResourceAsStream(configFile);
//        try {
//            params.load(is);
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//
//        driver=params.getProperty("driver");
//        url=params.getProperty("url");
//        user=params.getProperty("user");
//        password=params.getProperty("password");
//
//    }
}
